<?php

namespace app\store\model\server;

use app\common\model\server\User as UserModel;
use think\Db;

/**
 * 分销商用户模型
 * Class User
 * @package app\store\model\dealer
 */
class User extends UserModel
{
    /**
     * 获取分销商用户列表
     * @param string $search
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($search = '')
    {
        // 构建查询规则
        $this->alias('server')
            ->field('server.*, user.nickName, user.avatarUrl')
            ->join('user', 'user.user_id = server.user_id')
            ->where('server.is_delete', '=', 0)
            ->order(['server.create_time' => 'desc']);
        // 查询条件
        !empty($search) && $this->where('user.nickName|server.real_name|server.mobile', 'like', "%$search%");
        // 获取列表数据
        return $this->paginate(50, false, [
            'query' => \request()->request()
        ])->each(function ($item, $key) {

            if($item['is_parent'] == 0){
                $item['is_parent'] = '否';
            }else{
                $item['is_parent'] = '是';
            }
            $item['pid'] = Db::name('server_user')->where(['user_id'=>$item['pid']])->value('real_name');

        });
    }

    /**
     * 编辑分销商用户
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $data['type_ids'] = implode(',', $data['type_ids']);
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 删除分销商用户
     * @return mixed
     */
    public function setDelete()
    {
        return $this->transaction(function () {
            return $this->delete();
        });
    }

    /**
     * 资金冻结
     * @param $money
     * @return false|int
     */
    public function freezeMoney($money)
    {
        return $this->save([
            'money' => $this['money'] - $money,
            'freeze_money' => $this['freeze_money'] + $money,
        ]);
    }

    /**
     * 提现打款成功：累积提现佣金
     * @param $user_id
     * @param $money
     * @return false|int
     * @throws \think\exception\DbException
     */
    public static function totalMoney($user_id, $money)
    {
        $model = self::detail($user_id);
        return $model->save([
            'money' => $model['money'] - $money,
            'freeze_money' => $model['freeze_money'] + $money,
        ]);
    }

    /**
     * 确认打款
     * @param $user_id
     * @param $money
     * @return false|int
     * @throws \think\exception\DbException
     */
    public static function confirmMoney($user_id, $money)
    {
        $model = self::detail($user_id);
        if ($model['freeze_money'] < $money) return false;

        return $model->save([
            'freeze_money' => $model['freeze_money'] - $money,
            'total_money' => $model['total_money'] + $money,
        ]);
    }

    /**
     * 提现驳回：解冻分销商资金
     * @param $user_id
     * @param $money
     * @return false|int
     * @throws \think\exception\DbException
     */
    public static function backMoney($user_id, $money)
    {
        $model = self::detail($user_id);
        return $model->save([
            'money' => $model['money'] + $money,
            'total_money' => $model['total_money'] - $money,
        ]);
    }
}